package com.cy.service;

import com.cy.common.exception.DeleteException;
import com.cy.common.exception.ServiceException;
import com.cy.dao.SysNoticeDao;
import com.cy.dao.impl.SysNoticeDaoImpl;
import com.cy.dao.pojo.SysNotice;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告业务的具体实现
 * 1）核心业务（调用数据层）
 * 2）扩展业务（日志记录，权限控制，事务控制，缓存，）
 */

@Service
@Slf4j
public class SysNoticeServiceImpl implements SysNoticeService {
    //@Autowired
    private SysNoticeDao sysNoticeDao;
    //通过构造方法来进行依赖注入
    public SysNoticeServiceImpl(SysNoticeDao sysNoticeDao){
        this.sysNoticeDao = sysNoticeDao;
    }
    //初始化日志对象（org.slf4j.*）
//    private static final Logger log = LoggerFactory.getLogger(SysNoticeServiceImpl.class);

    @Override
    public List<SysNotice> selectNotice(SysNotice sysNotice) {
        //{}表示占位符
        log.debug("start: {}", System.currentTimeMillis());
        List<SysNotice> list = sysNoticeDao.selectNotice(sysNotice);
        log.debug("end: {}", System.currentTimeMillis());
        return list;
    }

    @Override
    public int deleteById(Long... id) {
        int row = sysNoticeDao.deleteById(id);
        if (row == 0 ){
            throw new DeleteException("记录可能已经被删除了");
        }
        return row;
    }

    @Override
    public SysNotice selectById(Long id) {
        SysNotice notice = sysNoticeDao.selectById(id);
        if (notice==null){
            throw  new ServiceException("记录可能已经被删了");
        }
        return notice;
    }

    @Override
    public int insertNotice(SysNotice sysNotice) {
        int row = sysNoticeDao.insertNotice(sysNotice);
        return row;
    }

    @Override
    public int updateNotice(SysNotice sysNotice) {
        int row = sysNoticeDao.updateNotice(sysNotice);
        return row;
    }
}
